// add-openid.js

var AddOpenID = {};

AddOpenID.load = function() {
    AppalachianDebug.trace("add-openid.js", "> load");
    var messages = document.getElementById("message-bundle");
    var box = document.getElementById("openid-url-textbox");
    if (window.arguments && window.arguments.length > 0 && window.arguments[0].url) {
        box.value = window.arguments[0].url;
        box.disabled = true;
        if(eval(window.arguments[0].server).length > 1) {
            document.getElementById("openid-server-textbox").value = messages.getString("multipleservers");
        } else {
            document.getElementById("openid-server-textbox").value = eval(window.arguments[0].server)[0];
        }
        document.getElementById("openid-server-textbox").disabled = true;
        document.getElementById("openid-server-serialized").value = window.arguments[0].server;
        document.getElementById("openid-use-textbox").focus();
    } else {
        box.focus();
    }
    AppalachianDebug.trace("add-openid.js", "< load");
};

AddOpenID.unload = function() {
    // do nothing
};

AddOpenID.cancel = function() {
    AppalachianDebug.trace("add-openid.js", "> cancel");
    if (window.arguments && window.arguments.length > 0 && window.arguments[0].cancelled)
        window.arguments[0].cancelled();
    window.close();
    AppalachianDebug.trace("add-openid.js", "< cancel");
};

AddOpenID.save = function() {
    AppalachianDebug.trace("add-openid.js", "> save");
    var messages = document.getElementById("message-bundle");
    try {
        var url = document.getElementById("openid-url-textbox").value;
        var use = document.getElementById("openid-use-textbox").value;
        var server = document.getElementById("openid-server-serialized").value;
        if (server == "") {
            server = [document.getElementById("openid-server-textbox").value];
        } else {
            server = eval(server);
        }

        if (url == "") {
            alert(messages.getString("blankopenid"));
        } else if (server == "") {
            alert(messages.getString("blankidserver"));
        } else if (use == "") {
            alert(messages.getString("blankusage"));
        } else {
            document.getElementById("ok-button").disabled = true;
            AppalachianDB.add(url, use, server, AddOpenID.checkDefault);
            if (window.arguments && window.arguments.length > 0 && window.arguments[0].saved)
                window.arguments[0].saved();
            window.close();
        }
    } catch (e) {
        alert(messages.getString("failure") + " " + e);
    }
    AppalachianDebug.trace("add-openid.js", "< save");
};

AddOpenID.checkDefault = function(id) {
    AppalachianDebug.trace("add-openid.js", "> checkDefault");
    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    var defaultID = prefs.getIntPref("extensions.appalachian.defaultID");
    var defaults = prefs.getBoolPref("extensions.appalachian.useDefault");
    if (defaults && defaultID == -1)
        prefs.setIntPref("extensions.appalachian.defaultID", id);
    AppalachianDebug.trace("add-openid.js", "< checkDefault");
};
